"""
数据准备页面
"""
import streamlit as st
from data.processors.cleaner import DataCleaner
from data.processors.transformer import DataTransformer


def show():
    """
    显示数据准备页面
    """
    st.title("数据准备")
    
    # 检查会话状态中是否有数据
    if "raw_data" not in st.session_state:
        st.warning("请先导入数据")
        return
    
    # 显示原始数据
    st.subheader("原始数据")
    st.dataframe(st.session_state.raw_data)
    
    # 数据清洗选项
    st.subheader("数据清洗")
    clean_options = st.multiselect(
        "选择清洗操作",
        options=["处理缺失值", "去除重复值", "标准化格式"],
        default=[]
    )
    
    if st.button("执行清洗"):
        cleaner = DataCleaner()
        cleaned_data = st.session_state.raw_data.copy()
        
        if "处理缺失值" in clean_options:
            cleaned_data = cleaner.handle_missing_values(cleaned_data)
        if "去除重复值" in clean_options:
            cleaned_data = cleaner.remove_duplicates(cleaned_data)
        if "标准化格式" in clean_options:
            cleaned_data = cleaner.standardize_format(cleaned_data)
        
        st.session_state.cleaned_data = cleaned_data
        st.success("数据清洗完成")
        st.dataframe(cleaned_data)
    
    # 数据转换选项
    st.subheader("数据转换")
    transform_options = st.multiselect(
        "选择转换操作",
        options=["数值标准化", "分类编码", "时间序列转换"],
        default=[]
    )
    
    if st.button("执行转换"):
        if "cleaned_data" not in st.session_state:
            st.warning("请先执行数据清洗")
            return
        
        transformer = DataTransformer()
        transformed_data = st.session_state.cleaned_data.copy()
        
        if "数值标准化" in transform_options:
            transformed_data = transformer.normalize_values(transformed_data)
        if "分类编码" in transform_options:
            transformed_data = transformer.encode_categories(transformed_data)
        if "时间序列转换" in transform_options:
            transformed_data = transformer.transform_timeseries(transformed_data)
        
        st.session_state.transformed_data = transformed_data
        st.success("数据转换完成")
        st.dataframe(transformed_data)